<template>
    <rx-layout>
        <template v-slot:header>
            <rx-filters :isBpmClient="false"
                        :isMyBpm="isMyBpmClient"
                        :readType="readType"
                        :readKey="readKey"
                        :treePrefix="treePrefix"
                        :keyname="getTreeCatByList.key" :namelit="getTreeCatByList.name"
                        :screenName="screenName"
                        @loadListByParams="loadListByParams"
                        :placeholder="i18n('RxMyApplication','请输入流程标题')"></rx-filters>
        </template>
        <template v-slot:center>
            <rx-grid ref="rxGrid" :queryParam="queryParam" :url="'/api-bpm/bpm/core/bpmInst/myBpmInsts'">
                <template v-slot:center="{record}">
                    <view class="rx-center" @click="bpmDetails(record)">
                        <view class="rx-html" v-if="getStatusHtml(record.status)"
                              v-html="getStatusHtml(record.status)"></view>
                        <view class="rx-createTime">
                            <my-icon style="color: rgba(66, 133, 244, 1);font-size: 1.2rem"
                                     type="icon-linetime"></my-icon>
                            <view class="rx-createTime-name">{{ record.updateTime }}</view>
                        </view>
                        <view class="rx-top">
                            <view class="rx-icon-le"><view class="rx-icon_liucheng"><my-icon type="icon-Vue3jpaas-icon_liucheng"></my-icon></view></view>
                            <view class="rx-subject">{{ record.subject }}</view>
                        </view>
                        <view class="rx-bottom">
                            <view class="rx-bottom-list">
                                <span class="rx-bottom-span">{{ i18n('orderNum', '业务单号') }}:</span>
                                <span class="rx-bottom-name">{{ record.busKey }}</span>
                            </view>
                            <view class="rx-bottom-list">
                                <span class="rx-bottom-span">{{ i18n('version', '版本') }}:</span>
                                <span class="rx-bottom-name">{{ record.version }}</span>
                            </view>
                        </view>
                        <template v-if="!['CANCEL'].includes(record.status)">
                            <view class="rx-operate" v-if="!['DRAFTED'].includes(record.status)">
                                <view class="rx-operate-button" @click.stop="bpmDelete(record)"
                                      v-if="checkRight('myDelBtn')">
                                    <my-icon type="icon-Vue3_delete"></my-icon>
                                    {{ $ti18('rxList.delBtn', '删除') }}
                                </view>
                                <view class="rx-operate-button" v-if="record.status == 'SUPSPEND'"
                                      @click="recovery.stop(record)">
                                    <my-icon type="icon-liuzhuanqingkuang"></my-icon>
                                    {{ i18n('Restore', '恢复') }}
                                </view>
                                <view class="rx-operate-button"
                                      v-if="record.status == 'RUNNING' && checkRight('suspendBtn')"
                                      @click.stop="suspend(record)">
                                    <my-icon type="icon-icon-test30"></my-icon>
                                    {{ i18n('suspend', '暂停') }}
                                </view>
                                <view
                                    class="rx-operate-button"
                                    v-if="record.status!='CANCEL'&&record.status!='ARCHIVED' && checkRight('cancelBtn')"
                                    @click.stop="bpmVoid(record)">
                                    <my-icon type="icon-lineVue3jpaas-icon_zuofei"></my-icon>
                                    {{ i18n('cancel', '作废') }}
                                </view>
                            </view>
                            <view class="rx-operate" v-else @click.stop="bpmDelete(record)">
                                <view class="rx-operate-button">
                                    <my-icon type="icon-Vue3_delete"></my-icon>
                                    {{ $ti18('rxList.delBtn', '删除') }}
                                </view>
                            </view>
                        </template>
                    </view>
                </template>
            </rx-grid>
        </template>
    </rx-layout>
</template>

<script>
import RxGrid from '@/components/RxGrid'
import RxLayout from '@/components/RxLayout'
import RxFilters from '@/components/RxFilters'
import instAndTaskShareApi from "@/api/instAndTaskShareApi";
import getStatusHtml from '@/common/getStatusHtml.js'
import GatewayApi from "@/api/Gateway/GatewayApi";

export default {
    name: "RxMyApplication",
    mixins: [getStatusHtml],
    components: {RxGrid, RxLayout, RxFilters},
    data() {
        return {
            isMyBpmClient: true,
            readKey: "task.read",
            readType: "instStartHist",
            treePrefix: 'i.',
            isInit: true,
            getTreeCatByList: '',
            queryParam: {},//筛选参数
            screenName: 'SUBJECT',
            curUser: {},
            //权限按钮
            buttons: [],
            //所有按钮
            allButtons: [],
        }
    },
    async created() {
        this.getTreeCatByKey()
        this.curUser = uni.getStorageSync("user");
        await this.initButton();
    },
    onShow(){
        if(this.$refs.rxGrid){
            this.$refs.rxGrid.getInit(true, true);
        }
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmLang", key);
        },
        getTreeCatByKey() {
            instAndTaskShareApi.getTreeCatByKey().then(ret => {
                this.getTreeCatByList = ret;
            })
        },
        //筛选
        loadListByParams(params) {
            this.queryParam = params
        },
        bpmDetails(obj) {
            let params = {
                instId: obj.instId,
                routerName: 'openDoc',
                action: 'detail',
                from: 'mystart'
            }
            let mobileAppId = uni.getStorageSync("mobileAppId");
            let appId = ''
            if (obj.appId) {
                appId = obj.appId ? obj.appId : mobileAppId
            }
            uni.navigateTo({
                url: '/views/modules/bpm/OpenDoc?params=' + JSON.stringify(params) + '&appId=' + appId
            })

        },
        //恢复
        recovery(record) {
            let self = this;
            uni.showModal({
                title: this.$ti18('rxMsa.tips', '提示'),
                content: this.i18n('resumeProcess', '是否恢复此流程'),
                confirmText: self.$ti18('rxForm.okBtn', '确定'),
                cancelText: self.$ti18('rxForm.cancelBtn', '取消'),
                success: function (res) {
                    if (res.confirm) {
                        GatewayApi.updateProcessStatus(record.instId, 'RUNNING').then(res => {
                            if (res.success) {
                                self.$refs.rxGrid.getInit(true, true)
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            });
        },
        //暂停
        suspend(record) {
            let self = this;

            uni.showModal({
                title: this.$ti18('rxMsa.tips', '提示'),
                content: this.i18n('suspendProcess', '是否暂停此流程'),
                confirmText: self.$ti18('rxForm.okBtn', '确定'),
                cancelText: self.$ti18('rxForm.cancelBtn', '取消'),
                success: function (res) {
                    if (res.confirm) {
                        GatewayApi.updateProcessStatus(record.instId, 'SUPSPEND').then(res => {
                            if (res.success) {
                                self.$refs.rxGrid.getInit(true, true)
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            });
        },
        //作废
        bpmVoid(record) {
            let self = this;
            uni.showModal({
                title:  this.$ti18('rxMsa.tips', '提示'),
                content: this.i18n('invalidate', '您确定需要作废该流程吗？'),
                confirmText: self.$ti18('rxForm.okBtn', '确定'),
                cancelText: self.$ti18('rxForm.cancelBtn', '取消'),
                success: function (res) {
                    if (res.confirm) {
                        var params = {
                            instId: record.instId,
                            reason: '作废'
                        }
                        GatewayApi.cancelProcess(params).then(res => {
                            if (res.success) {
                                self.$refs.rxGrid.getInit(true, true)
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            });
        },
        //删除我的申请
        bpmDelete(record) {
            let self = this;
            uni.showModal({
                title: this.$ti18('rxMsa.tips', '提示'),
                content: this.i18n('deleteProcess', '是否删除此流程'),
                confirmText: self.$ti18('rxForm.okBtn', '确定'),
                cancelText: self.$ti18('rxForm.cancelBtn', '取消'),
                success: function (res) {
                    if (res.confirm) {
                        GatewayApi.bpmInstDel(record.instId).then(res => {
                            if (res.success) {
                                self.$refs.rxGrid.getInit(true, true)
                            }
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            });
        },
        async initButton() {
            await GatewayApi.getFindAuthButton().then(res => {
                if (res.success) {
                    this.allButtons = res.data.allButtons;
                    this.buttons = res.data.buttons ? res.data.buttons : [];
                }
            })
        },
        /**
         * 检查按钮权限。
         * 如果当前用户有权限则直接返回，如果没有则检查全部按钮是否包含，如果包含则表示没有权限，否则有权限。
         * @returns {boolean}
         */
        checkRight(alias) {
            if (this.curUser && this.curUser.userId == '1') {
                return true;
            }
            //判断授权的按钮中是否有权限。
            let hasRight = this.hasKey(this.buttons, alias);

            if (hasRight) {
                return true;
            }
            //如果没有权限则去所有的按钮中查找一下，看看是否存在，如果存在则表示没有权限。
            //否则表示有权限。
            let rtn = this.hasKey(this.allButtons, alias);
            return !rtn;
        },
        hasKey(buttons, alias) {
            if(!buttons ||buttons.length==0){
                return false
            }
            for (let key in buttons) {
                let btns = buttons[key];
                if (btns && btns.includes(alias)) {
                    return true;
                }
            }
            return false;
        }
    }
}
</script>

<style scoped>
@import "../RxProcessManagement.css";
</style>
